FamilyPanel = Ext.extend(Ext.Panel, {
	constructor : function(config) {
		Ext.apply(this, config);
		this.tree = new Ext.tree.TreePanel({
			region : 'west',
			title : '管理',
			split : true,
			width : 200,
			minSize : 175,
			maxSize : 400,
			collapsible : true,
			layout : 'accordion',
			layoutConfig : {
				animate : true
			},
			autoScroll : true,
			animate : true,
			border : false,
			rootVisible : true,
			root : new Ext.tree.AsyncTreeNode({
				id : "root",
				text : "分类",
				"isWebnewscat" : "false",
				listeners : {
					'click' : function(node, event) {
						node.expand(true);
					}
				},
				loader : new RockyCurd.TreeLoader({
					dataUrl : "generateTree.action",
					expanded : true,
					nodeMeta : {
						id : 'id',
						text : 'showname'
					}
				})
			}),
			listeners : {
				'click' : function(node, event) {
					var panel = Ext.getCmp("NewsCurd");
					if (!panel) {
						Ext.MessageBox.alert('操作有误，新闻管理窗口打开失败！');
						return false;
					}
					panel.store.baseParams.newscatid = node.id;
					panel.refresh();
				}
			}
		});
		this.curd = new FamilyCurd();

	}
});
FamilyCurd = Ext.extend(RockyGridCurd, {
	title : "家庭信息管理",
	//layout : "fit",
	//region : "center",
	baseUrl : 'family.action',	
	winCfg : {
		height : 330,
		width : 360,
		maximizable : true,
		title : "家庭详细信息"
	},	
	conciseMode : true,
	trackMouseOver : false,
	loadMask:false, 
	autoWidth : false,		
	autoScroll:true,
	closable : true,	
	viewConfig : {
		forceFit : false
	},
	win : null,
	fp : null,
	constructor : function(config) {
		Ext.apply(this, config);
		this.keywordFilter = new Ext.form.TextField({
			width : 100,
			pressed : true,
			scope : this
		});
		this.memberstore = new Ext.data.JsonStore({
			autoLoad : false,
			id : "id",
			url : 'family.action?cmd=readone&suc=familymember',
			remoteSort : false,
			fields : ["id", "name"],
			listeners : {
				load : function(store, records, options) {
					store.insert(0, new Ext.data.Record({
						'id' : '-1',
						'name' : '请选择户主'
					}, '-1'));
					//this.roleFilter.setValue('-1');			    	   	
				},
				scope : this
			}
		});
		this.areatree = new Ext.tree.TreePanel({
			width : 560,
			height : 500,
			border : false,
			rootVisible : false,
			autoScroll : true,
			root : new Ext.tree.AsyncTreeNode({
				id : "-1",
				text : "未归类",
				expanded : true,
				loader : new RockyCurd.TreeLoader({
					dataUrl : "genAreaTree.action",
					expanded : true,
					nodeMeta : {
						id : 'id',
						text : 'showname'
					}
				})
			}),
			listeners : {
				click : function(node, e) {
					if( !node.isLeaf()){
					 	Ext.Msg.alert("提示","请选择行政村下的自然村！");
					 	return false;
					}
					var id = this.fp.find('name', 'id')[0].getValue();
					var address = this.fp.find('name', 'address')[0];
					if (address.getValue() == "") {
						address.setValue(node.attributes.areapath);
					}
					var treeCombox = this.fp.find("name", "area")[0]
					var hidden = this.fp.find('name', 'areaid')[0];
					treeCombox.setValue(node.text);
					hidden.setValue(node.id);
					this.areapopwin.hide();
					return true;
				},
				scope : this
			}
		});	
		this.store = new Ext.data.JsonStore({
			id : "id",
			url : 'family.action?cmd=read',
			root : "data",
			totalProperty : "rowCount",
			remoteSort : false,
			fields : ["area_code","areaid", "area", "householderid", "householdernameR", "householdername", "familycode", "address", "id", "createTime", "modifyTime", "keyword", "memo"],
			listeners : {
				beforeload : function() {
					this.store.baseParams.limit = 20;
					if (this.keyword) {
						this.keywordFilter.setValue(this.keyword);
					}
					var keywordFilterValue = this.keywordFilter.getValue();
					this.store.baseParams.keywordFilterValue = keywordFilterValue || '';
					
				},
				scope : this
			}
		});		
		var sm = new Ext.grid.CheckboxSelectionModel();
		var cmpId = this.id;
		this.cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer({
			header : ""
		}), sm, {
//			header : "id_family",
//			sortable : true,
//			width : 100,
//			dataIndex : "id"			
//		}, {	
//			header : "户主ID",
//			sortable : true,
//			width : 100,
//			renderer : function(v, r) {
//				if (!v || v == "-1") {
//					return "<font color=red>没有关联户主</font>";
//				} else {
//					return v;
//				}
//			},				
//			dataIndex : "householderid"
//		}, {			
//			header : "户主姓名",
//			sortable : true,
//			width : 100,
//			dataIndex : "householdername"
//		}, {
			header : "户主姓名R",
			sortable : true,
			width : 100,
			dataIndex : "householdernameR"
		}, {
			header : "家庭编码",
			sortable : true,
			width : 100,
			dataIndex : "familycode"
		}, {
			header : "自然村编码",
			sortable : true,
			width : 100,
			dataIndex : "area_code"				
		}, {
			header : "所在自然村",
			sortable : true,
			width : 200,
			dataIndex : "area"			
		}, {			
			header : "家庭地址",
			sortable : true,
			width : 400,
			dataIndex : "address"

		}]);
		this.cm.defaultSortable = true;
		FamilyCurd.superclass.constructor.call(this, {
			sm : sm,
			cm : this.cm,
			store : this.store,
			tbar : [{
				text : '添加',
				iconCls : 'add',
				pressed : false,
				handler : this.create,
				scope : this
			}, {
				text : '修改',
				iconCls : 'edit',
				pressed : false,
				handler : this.edit,
				scope : this
			}, {
				text : '删除',
				iconCls : 'remove',
				pressed : false,
				handler : this.removeData,
				scope : this
			}, {
				text : '刷新',
				iconCls : 'refresh',
				pressed : false,
				handler : this.refresh,
				scope : this
			}, new Ext.Toolbar.Fill(), '请输入关键字: ', this.keywordFilter, {
				text : '查询',
				iconCls : 'search',
				handler : this.search,
				scope : this
			}, {
				text : '重置',
				iconCls : 'cancel',
				pressed : true,
				handler : this.onReset,
				scope : this
			}, '   '],
			bbar : new Ext.PagingToolbar({
				pageSize : 20,
				store : this.store,
				displayInfo : true,
				displayMsg : '显示{0}-{1}，共{2}条记录',
				emptyMsg : "没有可显示的记录！"
			})
		});
		//this.store.load();		
	},
	showArchive : function(familyid) {
		this.fp = this.createForm();//基本信息
		this.photos = new AttachmentCurd({
			title : '土地照片',
			type : '1',
			familyid : familyid
		});//照片
		this.pics = new AttachmentCurd({
			title : '土地图纸',
			type : '2',
			familyid : familyid
		});//图纸
		this.archivePics = new AttachmentCurd({
			title : '原始档案',
			type : '3',
			familyid : familyid
		});//原始档案
		this.files = new AttachmentCurd({
			title : '备注文件',
			type : '4',
			familyid : familyid
		});//附件		
		this.archive = new Ext.Window({
			title : '农户档案详细信息',
			buttonAlign : "center",
			closeAction : "hide",
			layout : "fit",
			region : "center",
			width : 600,
			height : 360,
			modal : true,
			shadow : true,
			maximizable : true,
			items : {
				xtype : "tabpanel",
				activeTab : 0,
				defaults : {
					closable : false
				},
				items : [this.fp, this.photos, this.pics, this.archivePics, this.files]
			},
			buttons : [{
				text : "保存",
				handler : this.save,
				scope : this
			}, {
				text : "关闭",
				handler : this.closeWin,
				scope : this
			}]
		});
		this.archive.show();
		this.archive.maximize();
		var record = this.store.getById(familyid);
		this.fp.form.loadRecord(record);
		return false;
	},
	onReset : function() {
		this.keywordFilter.setValue("");
	},
	reset : function() {
		if (this.win) {
			this.fp.form.reset();
			this.memberstore.removeAll();
		}
	},
	edit : function() {
		var record = this.getSelectionModel().getSelected();
		if (!record) {
			Ext.Msg.alert("提示", "请先选择要编辑的行!");
			return;
		}
		var id = record.get("id");
		this.memberstore.reload({
			params : {
				id : id
			},
			callback : function() {
				this.showWin();
				this.fp.form.loadRecord(record);
				this.fp.find('name', 'householdernameR')[0].setValue(record.data.householdernameR);
				Ext.getDom('householderid').value = record.data.householderid;
			},
			scope : this
		});

		//alert(Ext.getDom('householderid').value);
		//		this.fp.find('name','householdernameR')[0].selectByValue(record.data.householderid);
	},
	createAreaPopwin : function() {
		if (!this.areapopwin) {
			this.areapopwin = new Ext.Window({
				width : 573,
				height : 545,
				autoScroll : false,
				title : '请选择所在自然村',
				buttonAlign : "center",
				closeAction : "hide",
				modal : true,
				shadow : true,
				items : this.areatree
			});
		}
		this.areapopwin.show(null,function(){
			if (this.areatree.rendered) {
				this.areatree.root.reload();
				this.areatree.root.expand(true);
			}		
		},this);
	},	
	createForm : function() {
		var formPanel = new Ext.form.FormPanel({
			xtype : "form",
			frame : true,
			autoScroll : true,
			border : false,
			bodyStyle : 'position:relative;padding:2px 0 2px 0px',
			defaults : {
				anchor : '90%'
			},
			items : [{
				xtype : "hidden",
				name : "id"
			}, {
				xtype : "hidden",
				name : "areaid"
			}, {
				xtype : 'combo',
				fieldLabel : '选择户主',
				name : 'householdernameR',
				hiddenName : "householderid",
				editable : true,
				triggerAction : 'all',
				valueField : 'id',
				displayField : 'name',
				mode : 'local',
				store : this.memberstore,
				allowBlank : true,
				listeners : {
					change : function(field, newValue, oldValue) {
						if (newValue != oldValue && newValue != "-1") {
							var householder = this.fp.find('name', 'householdername')[0];
							householder.setValue(field.store.getById(newValue).data.name);
						}
						return true;
					},
					scope : this
				}
			}, {
				xtype : 'textfield',
				fieldLabel : '户主姓名',
				name : "householdername",

				allowBlank : false

			}, {
				xtype : "trigger",
				fieldLabel : "选择自然村",
				allowBlank : false,
				emptyText : '请选择自然村',
				name : "area",
				editable : false,
				readOnly : true,
				onTriggerClick : function(e) {
					this.createAreaPopwin();
				}.createDelegate(this)
			}, {
				xtype : 'textfield',
				fieldLabel : '家庭编码',
				name : 'familycode',
				allowBlank : false
			}, {
				xtype : 'textarea',
				fieldLabel : '家庭地址',
				name : 'address',
				allowBlank : false
			}, {
				xtype : 'textarea',
				fieldLabel : '备注信息',
				name : 'memo',
				allowBlank : true
			}]
		});
		return formPanel;
	}
});